package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Issue;
import com.abaaba.project2.bean.IssueExample;
import com.abaaba.project2.bean.bo.AdminIssueBo;
import com.abaaba.project2.bean.vo.AdminIssueVo;
import com.abaaba.project2.mapper.IssueMapper;
import com.abaaba.project2.service.AdminIssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.java2d.pipe.SpanShapeRenderer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdminIssueServiceImpl implements AdminIssueService {
    @Autowired
    IssueMapper issueMapper;

    @Override
    public AdminIssueVo getIssueList(Integer page, Integer limit, String question, String sort, String order) {
        PageHelper.startPage(page, limit);
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort + " " + order);

        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (question != null && !"".equals(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }
        List<Issue> issueList = issueMapper.selectByExample(issueExample);

        PageInfo<Issue> issuePageInfo = new PageInfo<>(issueList);
        long total = issuePageInfo.getTotal();

        AdminIssueVo adminIssueVo = new AdminIssueVo();
        adminIssueVo.setItems(issueList);
        adminIssueVo.setTotal(total);
        return adminIssueVo;
    }

    @Override
    public Integer deleteIssue(Issue issue) {
        issue.setDeleted(true);

        return issueMapper.updateByPrimaryKey(issue);
    }

    @Override
    public Issue updateIssue(Issue issue) {
        IssueExample issueExample = new IssueExample();
        Issue issue1 = issueMapper.selectByPrimaryKey(issue.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        issue.setUpdateTime(simpleDateFormat.format(new Date()));
        int i = issueMapper.updateByPrimaryKey(issue);
        return issue;
    }

    @Override
    public Issue createIssue(AdminIssueBo adminIssueBo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Issue issue = new Issue(null, adminIssueBo.getQuestion(), adminIssueBo.getAnswer(),
                sdf.format(new Date()), sdf.format(new Date()), false);
        issueMapper.insert(issue);

        // 查询表中所有问题
        IssueExample issueExample = new IssueExample();
        List<Issue> issueList = issueMapper.selectByExample(issueExample);

        // 查出所有问题的最大 id
        int max = issueList.get(0).getId();
        for (Issue issue1 : issueList) {
            if (issue1.getId() > max) {
                max = issue1.getId();
            }
        }

        // 根据 id 查出刚刚新增的问题
        Issue issue1 = issueMapper.selectByPrimaryKey(max);
        return issue1;
    }
}
